package cn.easybuy.servlet.product;

import cn.easybuy.entity.Product;
import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/productCateServlet")
public class ProductCateServlet extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询所有商品分类
        try {
            List<ProductCategory> listProCate = productService.getALlCate();
            request.setAttribute("productCategoryVoList",listProCate);
            //查询分类下的所有商品列表
            for (ProductCategory category : listProCate) {
                List<Product> proDuctList = productService.getProDuctList(category.getId());
                category.setProductList(proDuctList);
            }
            request.getRequestDispatcher("pre/index.jsp").forward(request,response);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
